<template>
  <div class="app-container">
    <div class="head-container">
      <div style="display: flex;">
        <!-- 搜索 -->
        <label class="el-form-item-label">WMS任务号</label>
        <el-input v-model.trim="query.wmsTaskNo" clearable placeholder="WMS任务号" style="width: 185px;" class="filter-item" />
        <label class="el-form-item-label">是否裹膜</label>
        <el-select v-model="query.isWrapped" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;">
          <el-option
            v-for="item in dict.warp_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">是否打带</label>
        <el-select v-model="query.isStrapping" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;">
          <el-option
            v-for="item in dict.band_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <label class="el-form-item-label">任务状态</label>
        <el-select v-model="query.status" class="filter-item" clearable filterable placeholder="请选择" style="width: 185px;">
          <el-option
            v-for="item in dict.roller_task_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <div style="margin-left: 5px;">
          <el-button type="success" size="mini" class="filter-item" icon="el-icon-search">搜索</el-button>
          <el-button type="warning" size="mini" class="filter-item" icon="el-icon-refresh-right">重置</el-button>
        </div>
      </div>
      <!-- 列表渲染 -->
      <el-table ref="table" v-loading="loading" :data="list" size="small" style="width: 100%;">
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column prop="wmsTaskNo" label="WMS任务号" />
        <el-table-column prop="taskNo" label="滚筒线任务号" />
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            {{ dict.label.roller_task_status[scope.row.status] }}
          </template>
        </el-table-column>
        <el-table-column prop="startPoint" label="起点" />
        <el-table-column prop="endPoint" label="终点" />
        <el-table-column prop="isStrapping" label="是否打带">
          <template slot-scope="scope">
            {{ dict.label.band_status[scope.row.isStrapping] }}
          </template>
        </el-table-column>
        <el-table-column prop="isWrapped" label="是否裹膜">
          <template slot-scope="scope">
            {{ dict.label.warp_status[scope.row.isWrapped] }}
          </template>
        </el-table-column>
        <el-table-column prop="creationTime" label="创建时间" />
        <el-table-column label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <el-popconfirm
              confirm-button-text="确定"
              cancel-button-text="取消"
              icon="el-icon-info"
              icon-color="red"
              title="点击此按钮,将强制结束该任务,是否继续?"
              @confirm="forceEnd(scope.row, scope.$index)"
            >
              <el-button slot="reference" icon="el-icon-switch-button" type="danger" size="mini" :loading="btn.force === scope.$index">强制结束</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        :page-size.sync="query.page.size"
        :total="total"
        :current-page.sync="query.page.current"
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @size-change="sizeChangeHandler($event)"
        @current-change="pageChangeHandler"
      />
    </div>
  </div>
</template>

<script>
import { getRollerLineTaskListAPI, forceEndAPI } from '@/api/work/wcsMission'
export default {
  name: 'RollerLineTask',
  dicts: ['roller_task_status', 'band_status', 'warp_status'],
  data() {
    return {
      query: {
        wmsTaskNo: '',
        isWrapped: '',
        isStrapping: '',
        status: '',
        page: {
          current: 1,
          size: 20
        }
      },
      total: 0,
      loading: false,
      list: [
        // {
        //   creationTime: '2025-05-14 22:58:10', // 创建时间
        //   endPoint: 201, // 终点
        //   isStrapping: 0, // 是否打带
        //   isWrapped: 0, // 是否裹膜
        //   startPoint: 101, // 起点
        //   status: 1, // 状态:0 执行中 1:完成
        //   taskNo: 1, // 滚筒线任务号
        //   wmsTaskNo: 'WMS12345' // wms任务号
        // }
      ],
      btn: {
        force: ''
      }
    }
  },
  mounted() {
    this.toQuery()
  },
  methods: {
    toQuery() {
      this.query.page.current = 1
      this.getTable()
    },
    async getTable() {
      try {
        this.loading = true
        const res = await getRollerLineTaskListAPI(this.query)
        if (res.result === 'OK') {
          this.list = res.data.records
          this.total = res.data.total
        }
      } finally {
        this.loading = false
      }
    },
    sizeChangeHandler(e) {
      this.query.page.size = e
      this.query.page.current = 1
      this.getTable()
    },
    pageChangeHandler(e) {
      this.query.page.current = e
      this.getTable()
    },
    async forceEnd(row, index) {
      try {
        this.btn.force = index
        const res = await forceEndAPI({
          taskNo: row.taskNo,
          wmsTaskNo: row.wmsTaskNo
        })
        if (res.result === 'OK') {
          this.$message.success('强制结束成功')
          this.crud.toQuery()
        } else {
          this.$message.warning(res.data)
        }
      } finally {
        this.btn.force = ''
      }
    }
  }
}
</script>

<style scoped lang="scss">
</style>
